package com.woniuxy.lab.woniu_lab.component;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.lab.woniu_lab.model.Exper;
import com.woniuxy.lab.woniu_lab.model.User;
import com.woniuxy.lab.woniu_lab.service.ExperService;
import com.woniuxy.lab.woniu_lab.service.UserService;
import com.woniuxy.lab.woniu_lab.util.DockerUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Configuration
@EnableScheduling   // 2.开启定时任务
@Slf4j
public class ScheduleTask {

    @Resource
    private UserService userService;


    //每日0点检查会员过期情况(没参数)
    //cron生成网站https://www.matools.com/cron/
    @Scheduled(cron = "1 0 0 * * ?")
    private void inspectVipStatus(){
        log.info("{}执行操作",LocalDateTime.now());
        List<User> userList = userService.list(new QueryWrapper<User>().eq("vip", "VIP").lt("vip_time", LocalDateTime.now()));

        if (userList.size()>0){
            //存在过期的用户
            //修改状态
            List<User> enable = userList.stream().peek(o -> {
                o.setVip("NORMAL");
                o.setVipTime(LocalDateTime.of(1970,1,1,1,1,1));
            }).collect(Collectors.toList());
            userService.updateBatchById(enable);
        }else {
            log.info("今天没有会员到期");
        }
    }

}
